﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN32_2E : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {            
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            this.SetErrorMessage();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }    
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN32_1S.aspx");
    }
    #endregion
    #region "---------- Methods -----------"
    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";

        btnLoanObjective.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/VmLoanObjective.aspx") + "','700','550');");

        txtLoanObjective.AddControl(hidLoanObjective.ClientID);
        txtLoanObjective.AddControl(txtLoanObjective.ClientID);
        txtLoanObjective.ControlSearch = txtLoanObjective.ClientID;
        txtLoanObjective.AddQueryString("bIsActive", "1");
        txtLoanObjective.SearchKey = "VmLoanObjective_oCBT_CUST_BUSINESS_TYPE_ID_oCBT_DESC_LEV1";


        btnArrangementPurpose.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/ArrangementPurpose.aspx") + "','700','550');");

        txtArrangementPurpose.AddControl(hidLoanObjective.ClientID);
        txtArrangementPurpose.AddControl(txtLoanObjective.ClientID);
        txtArrangementPurpose.ControlSearch = txtLoanObjective.ClientID;
        txtArrangementPurpose.AddQueryString("bIsActive", "1");
        txtArrangementPurpose.SearchKey = "ArrangementPurpose_oArrangementPurposeID_oArrangementPurposeName";
    }
    private void BindData(int ID)
    {
        List<HPAndTFAddOn> hPAndTFAddOn = new List<HPAndTFAddOn>();        
        DataTable dt = new SPR_HPAndTFAddOn().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, hPAndTFAddOn);

            txtTransactionDate.Text = DateTime.Now.DateToString();
            ddlSourceData.SelectedValue = hPAndTFAddOn[0].SourceDataID.ToString();
            txtAccountNo.Text = hPAndTFAddOn[0].AccountNo;
            txtRefinanceDate.Text = hPAndTFAddOn[0].RefinanceDate.DateToString();
            txtMisPaymentDate.Text = hPAndTFAddOn[0].MisPaymentDate.DateToString();
            txtLoanObjective.Text = hPAndTFAddOn[0].LoanObjectiveName;
            txtArrangementPurpose.Text = hPAndTFAddOn[0].ArrangementPurposeName;
            ddlCreditLevel.SelectedValue = hPAndTFAddOn[0].CreditLevelID.ToString();
            ddlDoubtfulStatus.SelectedValue = hPAndTFAddOn[0].DoubtfulStatus.ToString();
            ddlOperationStatus.SelectedValue = hPAndTFAddOn[0].OperationStatus.ToString();
            ddlTDRType.SelectedValue = hPAndTFAddOn[0].TDRTypeID.ToString();
            ddlChangeCondition.SelectedValue = hPAndTFAddOn[0].ChangeConditionID.ToString();
            txtOldAccountNo.Text = hPAndTFAddOn[0].OldAccountNo;
            txtReservedAmount.Text = hPAndTFAddOn[0].ReservedAmount.ToString();
            txtUnpaidInterest.Text = hPAndTFAddOn[0].UnpaidInterest.ToString();

            hidHPAndTFAddOnID.Value = hPAndTFAddOn[0].HPAndTFAddOnID.ToString();
            hidUpdateDate.Value = hPAndTFAddOn[0].UpdatedDate.ToString();

            hidLoanObjective.Value = hPAndTFAddOn[0].LoanObjectiveID;
            hidArrangementPurpose.Value = hPAndTFAddOn[0].ArrangementPurposeID.ToString();
        }
    }
    private void BindDDL()
    {
        ddlSourceData.SetData(DDL_TYPE.SourceData, "", false, true);
        ddlCreditLevel.SetData(DDL_TYPE.CreditLevel, "", false, true);
        ddlDoubtfulStatus.SetData(DDL_TYPE.DoubtfulStatus, "", false, true);
        ddlOperationStatus.SetData(DDL_TYPE.OperationStatus, "", false, true);
        ddlTDRType.SetData(DDL_TYPE.TDRType, "", false, true);
        ddlChangeCondition.SetData(DDL_TYPE.ChangeCondition, "", false, true);
    }
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";       
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqSourceData.SetRequiredField(lblSourceData, ddlSourceData, btnSAVE);
        reqAccountNo.SetRequiredField(lblAccountNo, txtAccountNo, btnSAVE);
    }
    private void SaveData(bool IsConfirmed)
    {
        try
        {
            HPAndTFAddOn hPAndTFAddOn = new HPAndTFAddOn();

            hPAndTFAddOn.HPAndTFAddOnID = hidHPAndTFAddOnID.Value.AsInt32();
            hPAndTFAddOn.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
            hPAndTFAddOn.SourceDataID = ddlSourceData.SelectedValue.AsIntIsnull();
            hPAndTFAddOn.AccountNo = txtAccountNo.Text;
            hPAndTFAddOn.RefinanceDate = txtRefinanceDate.Text.StringToDateAllowNull();
            hPAndTFAddOn.MisPaymentDate = txtMisPaymentDate.Text.StringToDateAllowNull();
            hPAndTFAddOn.LoanObjectiveID = hidLoanObjective.Value;
            hPAndTFAddOn.ArrangementPurposeID = hidArrangementPurpose.Value.AsIntIsnull();
            hPAndTFAddOn.CreditLevelID = ddlCreditLevel.SelectedValue.AsIntIsnull();
            hPAndTFAddOn.DoubtfulStatus = ddlDoubtfulStatus.SelectedValue.AsIntIsnull();
            hPAndTFAddOn.OperationStatus = ddlOperationStatus.SelectedValue.AsIntIsnull();
            hPAndTFAddOn.TDRTypeID = ddlTDRType.SelectedValue.AsIntIsnull();
            hPAndTFAddOn.ChangeConditionID = ddlChangeCondition.SelectedValue.AsIntIsnull();
            hPAndTFAddOn.OldAccountNo = txtOldAccountNo.Text;
            hPAndTFAddOn.ReservedAmount = txtReservedAmount.Text.AsDecimal().Value;
            hPAndTFAddOn.UnpaidInterest = txtUnpaidInterest.Text.AsDecimal().Value;
            hPAndTFAddOn.SetStandardField();

            int iID = new SPR_HPAndTFAddOn().UPDATE(hPAndTFAddOn);

            if (iID > 0)
            {
                if (IsConfirmed)
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN32_3V.aspx?&id={0}", hPAndTFAddOn.HPAndTFAddOnID)));
                }
                else
                {
                    //this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN32_2E.aspx?&id={0}", hidHPAndTFAddOnID.Value.AsInt32())));
                    this.JS_Alert(Resources.ifrsResource.SaveData);
                    this.BindData(hidHPAndTFAddOnID.Value.AsInt32());
                }
            }
            else
            {
                if (iID == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN32_2E;
    }
    #endregion
}